<%@page language="java" contentType="text/html; charset=UTF-8" isELIgnored="false" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib prefix="html" tagdir="/WEB-INF/tags" %>
<!DOCTYPE html>
<html>
<html:header title="应收款">

    <link rel="stylesheet" href="${baseUrl}css/public.css" media="all">

    <style type="text/css">

    </style>

    <script>
        jQuery(function () {
            fixTableOperatorButton();
            ajaxSelect("${baseUrl}custom/customType/getAll.action","customTypeId","全部");
        });

        layui.use(['form', 'table'], function () {
            let $ = layui.jquery,
                form = layui.form,
                table = layui.table,
                util = layui.util;

            renderTable();

            table.on('toolbar(table)', function (obj) {
                let data = table.checkStatus('table').data;
                switch(obj.event){
                    case 'add':
                        openTab("新增收款单","${baseUrl}funds/arrearsOrderNew.action");
                        break;
                    case 'export':
                        exportExcel(data);
                        break;
                }
            });

            table.on('tool(table)', function (obj) {
                let data = obj.data;
                switch(obj.event){
                    case 'detail':
                        openTab(data.name+"收款详情","${baseUrl}funds/arrearsWaitDetail.action?customId="+data.id);
                        break;
                }
            });



            form.on('select(customTypeId)', function (data) {
                table.reload('table',{where: genSearch(),page: {curr: 1}});
            });

            form.on('checkbox(notNeedIn)', function (data) {
                table.reload('table',{where: genSearch(),page: {curr: 1}});
            });

            form.on('checkbox(customNoStop)', function (data) {
                table.reload('table',{where: genSearch(),page: {curr: 1}});
            });

            form.on('submit(search)', function (data) {
                table.reload('table',{where: genSearch(),page: {curr: 1}});
                return false;
            });
        });

        function renderTable() {
            $("#searchForm")[0].reset();
            layui.table.render({
                id:'table',
                elem: '#table',
                toolbar: '#tableToolBar',
                defaultToolbar:[],
                url: "${baseUrl}funds/arrearsWait/getList.action",
                totalRow:true,
                where : {
                    notNeedIn:"true",
                    customNoStop : "true",
                    time:(new Date()).valueOf()
                },
                cols: [[
                    {width:70,title:"序号",templet:function (d) {
                            return genTableIndex(d.status==2,'停用', d);
                        }},
                    {field:'storeName', width: 120, title: '所属门店',style:"font-size:12px;"},
                    {field:'no',width: 130, title: '客户编号',style:"font-size:12px;"},
                    {field:'name',width: 150, title: '客户名称',style:"font-size:12px;"},
                    {field:'contacts',width: 150, title: '联系人',style:"font-size:12px;"},
                    {field:'phone',width: 150, title: '联系电话',style:"font-size:12px;"},
                    {field:'init_amt', width: 120, title: '初期欠款',style:"font-size:12px;", templet: function(d){
                            return getPrice(d.init_amt);
                        }},
                    {field:'total', width: 120, title: '增加应收欠款',style:"font-size:12px;", templet: function(d){
                            return getPrice(d.total);
                        }},
                    {field:'hasIn', width: 120, title: '已付欠款',style:"font-size:12px;", templet: function(d){
                            return getPrice(d.hasIn);
                        }},
                    {field:'discount', width: 120, title: '优惠欠款',style:"font-size:12px;", templet: function(d){
                            return getPrice(d.discount);
                        }},
                    {field:'waitIn',width: 120, title: '应收欠款',style:"font-size:12px;",templet: function (d) {
                            return getPrice(parseFloat(d.waitIn)+parseFloat(d.init_amt));
                        }},
                    {title: '操作', minWidth: 80,fixed:'right', templet: '#operBar'},
                ]],
                limit: 10,
                page: { //支持传入 laypage 组件的所有参数（某些参数除外，如：jump/elem） - 详见文档
                    layout: ['limit', 'count', 'prev', 'page', 'next', 'skip'] //自定义分页布局
                    //,curr: 5 //设定初始在第 5 页
                    ,groups: 5 //只显示 5 个连续页码
                    ,first: false //不显示首页
                    ,last: false //不显示尾页
                },
                done: function(res, curr, count){
                    let init_amt = 0;
                    let total = 0;
                    let hasIn = 0;
                    let discount = 0;
                    let waitIn = 0;
                    $.each(res.data, function (i,d) {
                        init_amt += parseFloat(d.init_amt);
                        total += parseFloat(d.total);
                        hasIn += parseFloat(d.hasIn);
                        discount += parseFloat(d.discount);
                        waitIn += parseFloat(d.waitIn)+parseFloat(d.init_amt);
                    });

                    setTotalRow("init_amt",getPrice(init_amt));
                    setTotalRow("total",getPrice(total));
                    setTotalRow("hasIn",getPrice(hasIn));
                    setTotalRow("discount",getPrice(discount));
                    setTotalRow("waitIn",getPrice(waitIn));
                    setTotalRow("1","合计");

                    setTableLineCss("table",res.data, "status", 2, "font-gray-c2c2c2");
                }
            });
        }

        function genSearch() {
            let json = getFormJson($("#searchForm"));
            json.notNeedIn = $('[name="notNeedIn"]').next().hasClass("layui-form-checked") ? true:'';
            json.customNoStop = $('[name="customNoStop"]').next().hasClass("layui-form-checked") ? true:'';
            return json;
        }


        function exportExcel(data) {
            confirm("是否导出?",function () {
                tipLoad("处理中，请稍后");
                post("${baseUrl}funds/arrearsWait/export.action"+genParam(genSearch()),{},function (resp) {
                    closeAll();
                    if (resp.code == 1) {
                        downloadFileByForm("${baseUrl}file/excel/temp/"+resp.data);
                    }  else {
                        tip(resp.msg);
                    }
                });
            });
        }
    </script>

</html:header>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <form class="layui-form layui-form-pane" action="" id="searchForm">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <div class="layui-input-inline">
                        <input type="checkbox" name="notNeedIn" title="不显示无欠款客户" lay-skin="primary" value="true" checked lay-filter="notNeedIn">
                    </div>
                </div>

                <div class="layui-inline">
                    <div class="layui-input-inline">
                        <input type="checkbox" name="customNoStop" title="不显示停用客户" lay-skin="primary" value="1" checked lay-filter="customNoStop">
                    </div>
                </div>

                <div class="layui-inline" >
                    <label class="layui-form-label">客户分类</label>
                    <div class="layui-input-inline">
                        <select name="customTypeId" lay-filter="customTypeId"></select>
                    </div>
                </div>

                <div class="layui-inline">
                    <label class="layui-form-label">搜索</label>
                    <div class="layui-input-inline">
                        <input type="text" name="search" style="font-size: 12px;" placeholder="编号、客户、联系人、电话" autocomplete="off" class="layui-input">
                    </div>
                </div>


                <div class="layui-inline">
                    <a class="layui-btn" lay-submit="" lay-filter="search"><i class="layui-icon layui-icon-search"></i></a>
                </div>
            </div>
        </form>
        <hr/>

        <table class="layui-hide" id="table" lay-filter="table" lay-size="sm"></table>
    </div>
</div>
</body>

<script type="text/html" id="tableToolBar">
    <div class="layui-inline">
        <a class="layui-btn" lay-event="add">新增收款</a>
    </div>

    <div class="layui-inline">
        <a class="layui-btn layui-btn-primary" lay-event="export">导出</a>
    </div>
</script>

<script type="text/html" id="operBar">
    <a class="layui-btn layui-btn-xs layui-btn-normal table-operator" lay-event="detail">详情</a>
</script>

<html:loading/>
</html>